<template>
  <GiTable
    row-key="id"
    :data="dataList"
    :columns="columns"
    :loading="loading"
    :scroll="{ x: '100%', y: '100%' }"
    :pagination="pagination"
    :disabled-tools="['size', 'setting']"
    @refresh="search"
  >
    <template #toolbar-left>
      <a-input-search v-model="queryForm.title" placeholder="搜索标题" allow-clear @search="search" />
      <a-select
        v-model="queryForm.type"
        :options="todo_type"
        placeholder="请选择类型"
        allow-clear
        style="width: 150px"
        @change="search"
      />
      <a-button @click="reset">
        <template #icon><icon-refresh /></template>
        <template #default>重置</template>
      </a-button>
    </template>
    <template #title="{ record }">
      <a-link @click="onView(record)">
        <a-typography-paragraph
          class="link-text"
          :ellipsis="{
            rows: 1,
            showTooltip: true,
            css: true,
          }"
        >
          {{ record.title }}
        </a-typography-paragraph>
      </a-link>
    </template>
    <template #type="{ record }">
      <GiCellTag :value="record.type" :dict="todo_type" />
    </template>
    <template #priority="{ record }">
      <GiCellTag :value="record.priority" :dict="priority_type"/>
    </template>
    <template #businessType="{ record }">
      <GiCellTag :value="record.businessType" :dict="todo_business_enum"/>
    </template>
    <template #isRead="{ record }">
      <a-tag :color="record.isRead ? '' : 'arcoblue'">
        {{ record.isRead ? '已读' : '未读' }}
      </a-tag>
    </template>
  </GiTable>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import { type TodoQuery, type TodoResp, listUserTodo } from '@/apis/system'
import { useTable } from '@/hooks'
import { useDict } from '@/hooks/app'

defineOptions({ name: 'UserMyNotice' })

const {
  todo_method_enum,
  priority_type,
  todo_type,
  todo_status_enum,
  todo_business_enum,
  todo_scope_enum
} = useDict('todo_method_enum', 'priority_type', 'todo_type', 'todo_status_enum', 'todo_business_enum', 'todo_scope_enum')


const queryForm = reactive<TodoQuery>({
  sort: ['createTime,desc'],
})

const {
  tableData: dataList,
  loading,
  pagination,
  search,
} = useTable((page) => listUserTodo({ ...queryForm, ...page }), { immediate: true })

const columns: TableInstance['columns'] = [
  {
    title: '序号',
    width: 66,
    align: 'center',
    render: ({ rowIndex }) => h('span', {}, rowIndex + 1 + (pagination.current - 1) * pagination.pageSize),
  },
  { title: '待办标题', dataIndex: 'title', slotName: 'title', ellipsis: true, tooltip: true },
  { title: '优先级', dataIndex: 'priority', slotName: 'priority', align: 'center' },
  { title: '代办类型', dataIndex: 'type', slotName: 'type', align: 'center' },
  { title: '业务类型', dataIndex: 'businessType', slotName: 'businessType', align: 'center' },
  { title: '状态', dataIndex: 'isRead', slotName: 'isRead', align: 'center' },
  { title: '发布人', dataIndex: 'createUserString', ellipsis: true, tooltip: true },
  { title: '发送时间', dataIndex: 'createTime', width: 180 },
]

// 重置
const reset = () => {
  queryForm.title = undefined
  queryForm.type = undefined
  search()
}

const router = useRouter()
// 查看
const onView = (record: TodoResp) => {
  router.push({ path: '/user/todo', query: { id: record.id } })
}
</script>

<style scoped lang="scss"></style>
